AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')
	
ENT.Name = "Undead"
ENT.Hand = "anim_attachment_RH"
ENT.AnimScale = 2
ENT.Infect = true
ENT.Damage = 15
ENT.DoorDamage = 3
ENT.Pain = Vo.Undead_P
ENT.Death = Vo.Undead_D
ENT.Taunt = Vo.Undead_T
ENT.Attack = {"npc/fast_zombie/idle1.wav","npc/fast_zombie/idle1.wav"}
ENT.AttackAnims = {"attack01","attack01","attack02","attack02","attack03"}
ENT.AnimSpeeds = {.65, .65, .65, .65, .71}

for k,v in pairs({3,4,6,8}) do
	util.PrecacheModel("models/Zed/malezed_0"..v..".mdl")
end

function ENT:Initialize()

	util.PrecacheSound("npc/fast_zombie/idle1.wav")
	self:SetModel( "models/Zed/malezed_0"..ChooseRandom({4,6,8})..".mdl" )
	
	self:SetHullType( HULL_HUMAN );
	self:SetHullSizeNormal();
	
	self:SetSolid( SOLID_BBOX ) 
	self:SetMoveType( MOVETYPE_STEP )
	
	if GameInfo.JumpingZombies then
		self:CapabilitiesAdd( CAP_MOVE_GROUND | CAP_ANIMATEDFACE | CAP_TURN_HEAD | CAP_AIM_GUN | CAP_SKIP_NAV_GROUND_CHECK | CAP_SQUAD | CAP_MOVE_JUMP )
	else
		self:CapabilitiesAdd( CAP_MOVE_GROUND | CAP_ANIMATEDFACE | CAP_TURN_HEAD | CAP_AIM_GUN | CAP_SKIP_NAV_GROUND_CHECK | CAP_SQUAD | CAP_MOVE_CLIMB )
	end
	
	self:SetMaxYawSpeed( 5000 )
	
	//don't touch stuff above here
	self:SetHealth(math.random(35,45))
	self.MaxHealth = 55
	
	self.SkinNum = math.random(0,22)
	self:SetKeyValue( "skin", ""..self.SkinNum)

end

function ENT:SelectSchedule()

	local schdChase = ai_schedule.New( "AIFighter Chase" ) //creates the schedule used on this npc
	local slot = math.random(1,5)
	
	schdChase:AddTask( "FindEnemy", 		{ Class = "player", Radius = 200000 } )
	schdChase:EngTask( "TASK_GET_PATH_TO_ENEMY", 0 )
    schdChase:EngTask( "TASK_RUN_PATH_TIMED", 0.2 )
    schdChase:EngTask( "TASK_WAIT", 0.2 ) 
	
	local close = false
	local tbl = team.GetPlayers(TEAM_HUMANS)
	table.Add(tbl,ents.FindByClass("prop_door_rotating"))
	table.Add(tbl,ents.FindByModel("models/props_debris/wood_chunk03a.mdl"))
	table.Add(tbl,ents.FindByModel("models/props_debris/wood_chunk03b.mdl"))
	table.Add(tbl,ents.FindByClass("func_breakable"))
	for k,v in pairs(tbl) do
		if v:GetPos():Distance(self:GetPos()) < 100 then
			close = true
		end
	end

	if close then
		schdChase:EngTask( "TASK_STOP_MOVING", 0 )
		schdChase:EngTask( "TASK_FACE_ENEMY", 0 )
		schdChase:AddTask( "PlaySequence", 	{ Name = self.AttackAnims[slot], Speed = self.AnimSpeeds[slot] } )
	end
	
	self:StartSchedule( schdChase )
	
end

function ENT:DoDeath(dmginfo)

	if self.IsDead then self:Remove() return end
	self.IsDead = true

	local killer = dmginfo:GetAttacker()

	self:VoiceSound(ChooseRandom(self.Death),100)
			
	local ragdoll = ents.Create("prop_ragdoll")
	ragdoll:SetKeyValue("angles", self:GetAngles().p + math.random(-3,3).." "..self:GetAngles().y +  math.random(-3,3).." "..self:GetAngles().r + math.random(-3,3))
	ragdoll:SetKeyValue( "skin", ""..self.SkinNum)
	ragdoll:SetPos(self:GetPos())
	ragdoll:SetModel(self:GetModel())
	ragdoll:Spawn()
	ragdoll:Activate()
	ragdoll:Fire("kill",1,math.random(10,15))
	ragdoll:SetCollisionGroup( COLLISION_GROUP_DEBRIS )
	
	for i=1,20 do
		local bone = ragdoll:GetPhysicsObjectNum( i )
		if ValidEntity( bone ) then
			local bonepos, boneang = self:GetBonePosition( self:TranslatePhysBoneToBone( i ) )
			bone:SetPos( bonepos )
			bone:SetAngle( boneang )
		end
	end
	
	self:CalculateKiller(dmginfo,ragdoll)
	
	if self.FireAttacker then
		self.FireAttacker:AddAwardPoints(AWARD_GOOD_PYRO,1)
	end
			
	if math.random(1,100) <= GameInfo.ZombieItemChance then
		local hpos = ragdoll:LookupAttachment(self.Hand)
		local att = ragdoll:GetAttachment(hpos)
		if att == nil then
			att = {}
			att.Ang = Angle(0,0,0)
			att.Pos = ragdoll:GetPos() + Vector(0,0,5)
		end
		local item = ents.Create("prop_physics_respawnable")
		item:SetPos(att.Pos)
		item:SetAngles(att.Ang)
		item:SetModel(ChooseRandom(Inventory.ZombieItems))
		item:Spawn()
		item:Activate()
		item:SetHealth(150)
	end
			
	if math.random(1,4) == 1 then
		killer:VoiceSound(ChooseRandom(killer.Taunt),100,.3)
	end
			
	local phys = ragdoll:GetPhysicsObject()
	if phys:IsValid() then
		phys:SetMass(50)
		local fnum = 900
		if dmginfo:IsExplosionDamage() then 
			fnum = phys:GetMass()^3.2
		elseif GameInfo.ExaggeratedDeath then
			fnum = phys:GetMass()^2.2
		else
			fnum = phys:GetMass()^2.1
		end
		phys:AddAngleVelocity(Angle(math.Rand(-1*(fnum/5),fnum/5),math.Rand(-1*(fnum/3),fnum/3),math.Rand(-1*(fnum/5),fnum/5)))
		if self.OnFire then
			phys:ApplyForceCenter((ragdoll:GetPos()-killer:GetPos())*math.random(10,20))
			local effectdata = EffectData()
			effectdata:SetEntity(ragdoll)
			util.Effect("corpse_fire", effectdata)
		else
			phys:ApplyForceCenter((ragdoll:GetPos()-killer:GetPos())*math.random(50,200))
		end
	end
			
	self:SetSchedule( SCHED_FALL_TO_GROUND )
	self:Remove()
end